package com.lichongbing.lyoggl.controller;

import com.alibaba.fastjson.JSONObject;
import com.lichongbing.lyoggl.model.Subscription;
import com.lichongbing.lyoggl.service.SubscriptionService;
import com.lichongbing.lyoggl.utils.JwtTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lichongbing
 * @version 1.0.0
 * @createdate 2022/8/16 18:52
 * @description: TODO
 */
@Slf4j
@RestController
@RequestMapping("/store")
public class SubscriptionController {
    @Autowired
    private SubscriptionService subscriptionService;

    /**
     * 客户端订阅成功交易信息推送
     * @param jsonObject
     * @param request
     */
    @PostMapping("/create")
    public String create(@RequestBody JSONObject jsonObject, HttpServletRequest request){
        String token = request.getHeader("token");
        String userid = JwtTokenUtils.getUserid(token);
        String id = jsonObject.getString("id");
        String expir = jsonObject.getString("expir");
        String signDate = jsonObject.getString("signDate");
        Subscription subscription = new Subscription();
        subscription.setId(id);
        subscription.setUid(userid);
        subscription.setExpire(expir);
        subscription.setOderid(signDate);
        try{
            boolean save = subscriptionService.save(subscription);
            return String.valueOf(save);
        }catch(Exception e){
           return e.getMessage();
        }
    }
    @PostMapping("/push")
    public void push(@RequestBody JSONObject json,HttpServletRequest request){

    }
}
